<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>更新合同信息</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="laydate/need/laydate.css">
    <script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
    <script type="text/javascript" src="layer/layer.js"></script>
    <script type="text/javascript" src="js/handlebars-v3.0.3.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/mui.min.js"></script>
    <script type="text/javascript" src="laydate/laydate.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var hosturl = "http://"+ window.location.host + "/"+ window.location.pathname.split('/')[1];
            mui.ajax(hosturl+"/contract/byId", {
                data: {
                    "contractId": GetQueryString("id")
                },
                dataType: "json",
                type: "post",
                timeout: 10000,
                success: function (datas) {
                    if (datas.status != "ok") {
                        alert(datas.description);
                        window.location.href = 'updateContract.html';
                    } else {
                        var contractId = document.getElementById('contractId');
                        var contractName = document.getElementById('contractName');
                        var start = document.getElementById('start');
                        var deadline = document.getElementById('deadline');
                        var checkbox1 = document.getElementById('checkbox1');
                        var remind = document.getElementById('remind');
                        var times = document.getElementById('times');

                        contractId.value = datas.body.contractId;
                        contractName.value = datas.body.contractName;
                        start.value = datas.body.start;
                        deadline.value = datas.body.deadline;
                        if(!datas.body.warnOrNor){
                            checkbox1.checked = "";
                            document.getElementById('div1').style.display = 'none';
                        }else{
                            checkbox1.checked = true;
                            document.getElementById('div1').style.display = 'block';
                        }
                        remind.value = datas.body.remind;
                        times.value = datas.body.times;

                    }
                },
                error: function (xhr, type, errorThrown) {
                    console.log(type);
                }
            });
        });
    </script>
</head>

<body>
<div class="addMain" style="margin:0px auto;width:300px;height:300px;">
    <div class="add-detail">
        合同编码：<input type="text" id="contractId" class="add-text"/>
    </div>
    <div class="add-detail">
        合同名称：<input type="text" id="contractName" class="add-text"/>
    </div>
    <div class="add-detail-date">
        起始时间：<input type="text" id="start" class="laydate-icon" onclick="laydate()"/>
    </div>
    <div class="add-detail-date">
        截止时间：<input type="text" id="deadline" class="laydate-icon" onclick="laydate()"/>
    </div>
    <div>
        付款期数：<input type="text" id="times" class="add-text" disabled="disabled "/>
    </div>
    <div>
        是否提醒：<input id="checkbox1" type="checkbox"/>
    </div>
    <div class="add-detail-date" id="div1" style="display:block;margin-top:10px;">
        提醒时间：<input type="text" id="remind" class="laydate-icon" onclick="laydate()"/>
    </div>
    <div class="" style="display:none">
        占位：<input type="text" class="laydate-icon" onclick="laydate()"/>
    </div>
    <div class="">
        <input type="button" id="btn" class="add-receive"
               style="height:23px;width:84px;line-height:20px;font-size:12px;" value="保存"/>
    </div>
</div>
</body>
</html>
<script type="text/javascript">
    var hosturl = "http://"+ window.location.host + "/"+ window.location.pathname.split('/')[1];
    window.onload = function () {
        var oCheckbox = document.getElementById('checkbox1');
        var oDiv1 = document.getElementById('div1');
        oCheckbox.addEventListener("click", function () {
                    if (oCheckbox.checked == true) {
                        oDiv1.style.display = 'block';
                    }
                    else {
                        oDiv1.style.display = 'none';
                    }
                }
        );
        var btn = document.getElementById('btn');
        btn.addEventListener("click", function () {
            var contractId = document.getElementById('contractId').value;
            var contractName = document.getElementById('contractName').value;
            var start = document.getElementById('start').value;
            var deadline = document.getElementById('deadline').value;
            var checkbox1 = document.getElementById('checkbox1');
            var remind = document.getElementById('remind').value;
            var times = document.getElementById('times').value;

            if(contractId==""){
                layer.msg('合同编码不能为空！', function(){
                });
                return;
            }
            if(contractName==""){
                layer.msg('合同名称不能为空！', function(){
                });
                return;
            }
            if(start==""){
                layer.msg('开始时间不能为空！', function(){
                });
                return;
            }
            if(deadline==""){
                layer.msg('截止时间不能为空！', function(){
                });
                return;
            }
            if(checkbox1.checked && remind==""){
                layer.msg('请输入提醒时间！', function(){
                });
                return;
            }
            if(remind!="" && deadline!="" && remind>deadline){
                layer.msg('提醒时间不能大于截止时间！', function(){
                });
                return;
            }
            if(!checkbox1.checked){
                remind = "2000-01-01";
            }

            mui.ajax(hosturl+"/contract/update", {
                data: {
                    "contractId": contractId,
                    "contractName": contractName,
                    "start": start,
                    "deadline": deadline,
                    "warnOrNor": checkbox1.checked,
                    "remind": remind,
                    "times": times,
                    "id": GetQueryString("id")
                },
                dataType: "json",
                type: "post",
                timeout: 10000,
                success: function (datas) {
                    if (datas.status != "ok") {
                        alert(datas.description);
                        window.location.href = hosturl+'/updateContract.html?id=' + GetQueryString("id");
                    } else {
                        parent.$("#handle_status").val('OK');
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }
                },
                error: function (xhr, type, errorThrown) {
                    console.log(type);
                }
            });
        });
    };

    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)return unescape(r[2]);
        return null;
    };
</script>

<script>
    !function () {
        laydate({
            elem: '#demo'
        })
    }();
</script>
</body>
</html>